package io.gps.datatransfer.task.taskentity;

import io.gps.datatransfer.CenterChannel;
import io.gps.datatransfer.command.SendMsgPack;
import org.apache.log4j.Logger;

public class CenterConnWork implements Runnable  {

    private Logger logger = Logger.getLogger(CenterConnWork.class);
    CenterChannel centerChannel;
    /**
     * 批量执行数据解析
     * @param centerChannel
     */
    public CenterConnWork(CenterChannel centerChannel) {
        this.centerChannel = centerChannel;
    }
    @Override
    public void run() {
        try {
            if(centerChannel.cssession==null||!centerChannel.cssession.isConnected()){
                centerChannel.connectServer();
                centerChannel.lastHandTime=System.currentTimeMillis();
            }else {
                if(centerChannel.cssession!=null&&centerChannel.cssession.isConnected()){
                    if(centerChannel.isSendSta){
                        if (System.currentTimeMillis()-centerChannel.lastHandTime>=30000){
                            centerChannel.cssession.write(SendMsgPack.get0002(centerChannel.sim));
                            // 发送心跳
                            centerChannel. lastHandTime=System.currentTimeMillis();
                        }
                        centerChannel.sendMsg();
                    }else if(System.currentTimeMillis()-centerChannel.lastHandTime>600*1000){
                        // 超过10 分钟 无通信 ，中断请求 重新链接
                        logger.info( " 超过10 分钟 无通信 ，中断请求 重新链接--------------------------------销毁回话"+centerChannel.devServer.getServerid());
                        centerChannel.disconnect("CenterConnWork 超过10 分钟 无通信 关闭回话");
                    }
                }else{
                    centerChannel.disconnect("CenterConnWork 回话已经被断开");
                }
            }
        } catch (Exception ie) {
            logger.error(ie.getMessage(),ie);
        }
    }
}